package org.serverct.finalfight.game.hadesgame.events;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.hadesgame.ContinuousEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ArrowGround extends ContinuousEvent {

    public ArrowGround(BasicGame game, EventType type) {
        super(game, type);
    }

    @Override
    public void tick() {
        List<UUID> arrows = new ArrayList<>();
        for (UUID uuid : this.game.players.keySet()) {
            Player player = Bukkit.getPlayer(uuid);
            if (!this.game.isAlive(player)) {
                continue;
            }
            Location loc = player.getLocation();
            World world = player.getWorld();
            for (int degree = 0; degree < 360; degree += 15) {
                double radians = Math.toRadians(degree);
                double x = Math.cos(radians);
                double y = Math.sin(radians);
                Location current_1 = loc.clone().add(x * 1.5, 2, y * 1.5);
                Arrow arrow_1 = world.spawnArrow(current_1, new Vector(0, -1, 0), 1f, 12);
                arrows.add(arrow_1.getUniqueId());
                Location current_2 = loc.clone().add(x * 3, 2, y * 3);
                Arrow arrow_2 = world.spawnArrow(current_2, new Vector(0, -1, 0), 1f, 12);
                arrows.add(arrow_2.getUniqueId());
            }
        }
        new BukkitRunnable() {
            @Override
            public void run() {
                for (UUID uuid : arrows) {
                    Entity entity = Bukkit.getEntity(uuid);
                    if (entity != null) {
                        entity.remove();
                    }
                }
            }
        }.runTaskLater(FinalFight.plugin, 25L);
    }
}
